﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _2QWebProxy.App_Code;

namespace _2QWebProxy.GUI
{
    public partial class ucPrivacyDomain : UserControl
    {
        public bool isDenyForm = false;

        public ucPrivacyDomain(bool isDenyForm)
        {
            this.isDenyForm = isDenyForm;
            InitializeComponent();
            LoadPrivacyDomain();
        }

        private void LoadPrivacyDomain()
        {
            if (isDenyForm)
            {
                List<PrivacyDomain> domains = Utils.GetPrivacyDomain("deny");
                dgvPrivacyDomain.DataSource = domains;
            }
            else
            {
                List<PrivacyDomain> domains = Utils.GetPrivacyDomain("allow");
                dgvPrivacyDomain.DataSource = domains;
            }
        }

        private void SaveDomain(string status)
        {
            PrivacyDomain domain = new PrivacyDomain()
            {
                Name = txtDomainName.Text,
                Status = status
            };
            Utils.SavePrivacyDomain(domain);
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            Utils.DeleteAllPrivacyDomain();
            LoadPrivacyDomain();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.ParentForm.Close();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            PrivacyDomain domain = new PrivacyDomain()
            {
                Name = txtDomainName.Text
            };

            Utils.DeletePrivacyDomain(domain);
            LoadPrivacyDomain();
        }

        private void dgvPrivacyDomain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvPrivacyDomain.SelectedCells[0].RowIndex;
            txtDomainName.Text = dgvPrivacyDomain.Rows[index].Cells[0].Value.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtDomainName.Text == "")
            {
                MessageBox.Show("Invalid website Name.");
                return;
            }

            if (isDenyForm)
            {
                SaveDomain("deny");
            }
            else
            {
                SaveDomain("allow");
            }
            LoadPrivacyDomain();
        }
    }
}
